package cdu.zch.healthcare;

import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import java.util.HashMap;
import java.util.List;

import cdu.zch.healthcare.Database;
import cdu.zch.healthcare.R;

public class AppointmentAdapter extends BaseAdapter {
    private Context context;
    private List<HashMap<String, String>> data;
    private LayoutInflater inflater;
    private Database db;

    public AppointmentAdapter(Context context, List<HashMap<String, String>> data, Database db) {
        this.context = context;
        this.data = data;
        this.inflater = LayoutInflater.from(context);
        this.db = db;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.multi_lines_appointment, parent, false);
            holder = new ViewHolder();
            holder.nameTextView = convertView.findViewById(R.id.name);
            holder.timeTextView = convertView.findViewById(R.id.time);
            holder.cancelButton = convertView.findViewById(R.id.cancel);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        HashMap<String, String> item = data.get(position);
        holder.nameTextView.setText(item.get("line1"));
        holder.timeTextView.setText(item.get("line2"));

        holder.cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(context)
                        .setTitle("取消预约")
                        .setMessage("确定要取消这个预约吗？")
                        .setPositiveButton("是", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // 处理取消预约逻辑
                                HashMap<String, String> appointment = data.get(position);
                                String[] h=appointment.get("line2").split(" ");
                                db.cancelAppointment(appointment.get("line1"), h[0]);
                                data.remove(position);
                                notifyDataSetChanged(); // 刷新视图
                                Toast.makeText(context, "预约已取消", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNegativeButton("否", null)
                        .show();
            }
        });

        return convertView;
    }

    static class ViewHolder {
        TextView nameTextView;
        TextView timeTextView;
        Button cancelButton;
    }
}
